﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace project6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private static int flag = 0;//一个标志位
        private static int flushtag = 0;
        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //1
            if (!Judge())
            {
                textBox2.Text = textBox2.Text + "1";
            }
            else if(Judge())
            {
                if (flushtag != 1)
                {
                    textBox2.Text = "";
                }
                flushtag = 1;
                textBox2.Text = textBox2.Text + "1";
            }
            if (!Judge())
            {
                textBox1.Text = textBox1.Text + "1";
            }


        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (!Judge())
            {
                textBox2.Text = textBox2.Text + "2";
            }
            else
            {
                if (flushtag != 1)
                {
                    textBox2.Text = "";
                }
                flushtag = 1;
                textBox2.Text = textBox2.Text + "2";
            }
            if (!Judge())
            {
                textBox1.Text = textBox1.Text + "2";
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (!Judge())
            {
                textBox2.Text = textBox2.Text + "3";
            }
            else
            {
                if (flushtag != 1)
                {
                    textBox2.Text = "";
                }
                flushtag = 1;
                textBox2.Text = textBox2.Text + "3";
            }
            if (!Judge())
            {
                textBox1.Text = textBox1.Text + "3";
            }
        }

        private void button20_Click(object sender, EventArgs e)
        {
            //等于按钮，结算前面的计算
            if (flushtag == 1)
            {
                prenum = getresult();
                //text1重组
                //textBox1.Text = prenum.ToString() + textBox1.Text[textBox1.Text.Length - 1];
                textBox1.Text = textBox1.Text + textBox2.Text;
                textBox2.Text = prenum.ToString();
            }

        }

        private void button13_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
        }
        string tempstring = "";
        private bool Judge()
        {
            char ch = getlast();
            if (ch == '➕'|| ch == '➖' || ch == '✖' || ch == '➗')
            {
            return true;
            }
            return false;
        }
        private char getlast()
        {
            //获取最后一个字符
            if (textBox1.Text != "")
            { char ch = textBox1.Text[textBox1.Text.Length - 1];
                return ch; }

            return '0';
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (!Judge())
            {
                textBox2.Text = textBox2.Text + "4";
            }
            else
            {
                if (flushtag != 1)
                {
                    textBox2.Text = "";
                }
                flushtag = 1;
                textBox2.Text = textBox2.Text + "4";
            }
                if (!Judge())
            {
                textBox1.Text = textBox1.Text + "4";
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (!Judge())
            {
                textBox2.Text = textBox2.Text + "5";
            }
            else
            {
                if (flushtag != 1)
                {
                    textBox2.Text = "";
                }
                flushtag = 1;
                textBox2.Text = textBox2.Text + "5";
            }
                if (!Judge())
            {
                textBox1.Text = textBox1.Text + "5";
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (!Judge())
            {
                textBox2.Text = textBox2.Text + "6";
            }
            else
            {
                if (flushtag != 1)
                {
                    textBox2.Text = "";
                }
                flushtag = 1;
                textBox2.Text = textBox2.Text + "6";
            }
                if (!Judge())
            {
                textBox1.Text = textBox1.Text + "6";
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (!Judge())
            {
                textBox2.Text = textBox2.Text + "7";
            }
            else
            {
                if (flushtag != 1)
                {
                    textBox2.Text = "";
                }
                flushtag = 1;
                textBox2.Text = textBox2.Text + "7";
            }
                if (!Judge())
            {
                textBox1.Text = textBox1.Text + "7";
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (!Judge())
            {
                textBox2.Text = textBox2.Text + "8";
            }
            else
            {
                if (flushtag != 1)
                {
                    textBox2.Text = "";
                }
                flushtag = 1;
                textBox2.Text = textBox2.Text + "8";
            }
                if (!Judge())
            {
                textBox1.Text = textBox1.Text + "8";
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (!Judge())
            {
                textBox2.Text = textBox2.Text + "9";
            }
            else
            {
                if (flushtag != 1)
                {
                    textBox2.Text = "";
                }
                flushtag = 1;
                textBox2.Text = textBox2.Text + "9";
            }
                if (!Judge())
            {
                textBox1.Text = textBox1.Text + "9";
            }
        }

        private void button11_Click(object sender, EventArgs e)
        {
            if (!Judge())
            {
                textBox2.Text = textBox2.Text + "0";
            }
            else
            {
                if (flushtag != 1)
                {
                    textBox2.Text = "";
                }
                flushtag = 1;
                textBox2.Text = textBox2.Text + "0";
            }
            if (!Judge())
            {
                textBox1.Text = textBox1.Text + "0";
            }
        }

        private void button19_Click(object sender, EventArgs e)
        {   
            //利用judge函数判断txet1中最后一位是数字还是符号
            if (Judge())
            {  //删去最后一位符号，再添加上新符号
                textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
                textBox1.Text = textBox1.Text + "➕";
            }
            else if(!Judge())
            {   //直接添加符号
                textBox1.Text = textBox1.Text + "➕";
            }
            if (flushtag == 1)
            {
                prenum = getresult();
                //text1重组
                textBox1.Text = prenum.ToString() + textBox1.Text[textBox1.Text.Length - 1];
            }
        }

        private void button18_Click(object sender, EventArgs e)
        {
            //利用judge函数判断txet1中最后一位是数字还是符号
            if (Judge())
            {  //删去最后一位符号，再添加上新符号
                textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
                textBox1.Text = textBox1.Text + "➖";
            }
            else if (!Judge())
            {   //直接添加符号
                textBox1.Text = textBox1.Text + "➖";
            }
            if (flushtag == 1)
            {
                prenum = getresult();
                //text1重组
                textBox1.Text = prenum.ToString() + textBox1.Text[textBox1.Text.Length - 1];
            }
        }

        private void button17_Click(object sender, EventArgs e)
        {
            //利用judge函数判断txet1中最后一位是数字还是符号
            if (Judge())
            {  //删去最后一位符号，再添加上新符号
                textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
                textBox1.Text = textBox1.Text + "✖";
            }
            else if (!Judge())
            {   //直接添加符号
                textBox1.Text = textBox1.Text + "✖";
            }
            if (flushtag == 1)
            {
                prenum = getresult();
                //text1重组
                textBox1.Text = prenum.ToString() + textBox1.Text[textBox1.Text.Length - 1];
            }
        }

        private void button16_Click(object sender, EventArgs e)
        {
            //利用judge函数判断txet1中最后一位是数字还是符号
            if (Judge())
            {  //删去最后一位符号，再添加上新符号
                textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
                textBox1.Text = textBox1.Text + "➗";
            }
            else if (!Judge())
            {   //直接添加符号
                textBox1.Text = textBox1.Text + "➗";
            }
            if (flushtag == 1)
            {
                prenum = getresult();
                //text1重组
                textBox1.Text = prenum.ToString() + textBox1.Text[textBox1.Text.Length - 1];
            }
        }
        string s1, s2;
        double prenum;

        private void button15_Click(object sender, EventArgs e)
        {
            if(flushtag == 1)
            {
                textBox2.Text = textBox2.Text.Substring(0, textBox2.Text.Length - 1);
            }
            else if(flushtag == 0)
            {
                if (Judge())
                {
                    textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
                }
                else if(!Judge())
                {
                    textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
                    textBox2.Text = textBox2.Text.Substring(0, textBox2.Text.Length - 1);
                }
            }
        }

        private double getresult()
        {
            char ch;
            double num1=0;
            double num2=0;
            //if (flag == 0)
            //{
            //    ch = textBox1.Text[textBox1.Text.Length - 1];
            //    s1 = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
            //    s2 = textBox2.Text;
            //    num1 = int.Parse(s1);
            //    num2 = int.Parse(s2);
            //
            //if (flag > 0)
            //{
            ch = textBox1.Text[textBox1.Text.Length - 1];
            s1 = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
            s2 = textBox2.Text;
            num1 = int.Parse(s1);
            num2 = int.Parse(s2);
            flushtag = 0;
            if (ch == '➕')
                {
                    return num1 + num2;
                }else if (ch == '➖')
                {
                    return num1 - num2;
                }
                else if (ch == '✖')
                {
                    return num1 * num2;
                }
                else if (ch == '➗')
                {
                    return num1 / num2;
                }
//            }
            flushtag = 0;
            return num1;
        }
    }
}
